﻿/* $RCSFile: Selector.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/Selector.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using AddonNice.Core;
using AddonNice.Configuration;
using System.ComponentModel;
using System.ComponentModel.Design;
using AddonNice.Helpers;
using AddonNice.Settings;

namespace AddonNice.UI.WebControls
{
	/// <summary>
	/// This user control will render the current list of languages
    /// On change in list, do a redirect, setting lang querystring according new language
	/// </summary>
	[ToolboxData("<{0}:Selector runat='server'></{0}:Selector>")]
	public class Selector : LangChooser 
	{ 
		public Selector ()
		{
			ImagePath                   =   Path.ToAbsolute(Config.PortalFlagsPath);
			ChangeLanguageAction        =   LangChooserAction.CallDelegate;
			LanguageListString          =   Config.DefaultLanguage;//"en-US";
			// Obtain PortalSettings from Current Context
		    PortalSettings pS           =   (PortalSettings) HttpContext.Current.Items[StStr.strPortalSettings];
			if ( HttpContext.Current != null && pS != null)
			{
				if (pS.CustomSettings.ContainsKey(StStr.SiteSettingsLangList))
					LanguageListString  =   pS.CustomSettings[StStr.SiteSettingsLangList].Value;
			}
		}

		/// <summary>
		/// Examines/combines all the variables involved and sets
		/// CurrentUICulture and CurrentCulture.
		/// </summary>
		protected override void OnChangeLanguage(LangChooserEventArgs e)
		{
			if ( Context == null)
                return;
			int mID                 =   0;
			if (Context.Request.Params[StStr.strModuleID] != null)
				mID                 =   int.Parse(Context.Request.Params[StStr.strModuleID]);

			int tID                 =   0;
            if (Context.Request.Params[StStr.strPageID] != null)
                tID                 =   int.Parse(Context.Request.Params[StStr.strPageID]);
            else if (Context.Request.Params[StStr.strTabID] != null)
				tID                 =   int.Parse(Context.Request.Params[StStr.strTabID]);

			string auxUrl           =   HttpRuntime.AppDomainAppPath;
			string auxApplication   =   HttpRuntime.AppDomainAppVirtualPath;
			int index               =   auxUrl.ToLower().IndexOf(auxApplication.ToLower());
			if (index != -1) 
			{
				auxUrl              =   auxUrl.Substring(index + auxApplication.Length);
			}
			if (auxUrl.StartsWith("/"))
				auxUrl              =   "~" + auxUrl;
			else
				auxUrl              =   "~/" + auxUrl;
			
            StringBuilder sb        =   new StringBuilder();
			foreach (string key in Context.Request.QueryString.Keys) 
			{
                string k            =   key.ToLower();
                if (!k.Equals(StStr.strModuleID) && !k.Equals(StStr.strPageID) && 
                    !k.Equals(StStr.strTabID) && !k.Equals("lang"))
                    sb.AppendFormat("&{0}={1}", key, Context.Request.Params[key]);
			}
            string customParams     =   sb.ToString();

			customParams            +=  "&lang=" +  e.CultureItem.Culture.Name;
			string returnUrl        =   HttpUrlBuilder.BuildUrl (auxUrl,DynPageLoc.Normal, tID, mID, e.CultureItem.Culture, customParams, string.Empty, string.Empty);
            if ( Page is ADNPage )
			    ((ADNPage)Page).Redirect(returnUrl,true);
			else Context.Response.Redirect(returnUrl);
		}
	}
}